package servlet.client;

import dao.domain.Client;
import service.ClientService;
import service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author gzb
 * @Date 2023/9/20
 * @Description ${PACKAGE_name}
 */
@WebServlet(name = "ClientEditServlet", value = "/ClientEditServlet")
public class ClientEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String account = UserService.getAccountFromCookie(request);
        String userType = "client";
        String msg;
        try {
            if (UserService.selectByAccount(userType, account) != null) {
                Client client = (Client) UserService.selectByAccount("client", account);
                client.setPassword(password);
                client.setName(name);
                ClientService.update(client);
                msg = "{\"msg\":\"success\"}";
            } else {
                msg = "{\"msg\":\"fail\"}";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        response.getWriter().write(msg);
        System.out.println("finish");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ClientEditServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
